<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <!--bootstrap-->
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/js/bootstrap/css/bootstrap.min.css"/>
    <!--bootstrap-toastr-->
    <script type="text/javascript" src="/js/toastr/toastr.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/js/toastr/toastr.css"/>
</head>
<body>
<form class="form-horizontal">
    <div class="form-group">
        <input type="hidden" name="id" id="id" th:value="${admin.id}">
    </div>
    <div class="form-group">
        <label for="username" class="col-sm-2 control-label">名称</label>
        <div class="col-sm-6">
            <input type="text" th:value="${admin.username}" class="form-control" id="username" placeholder="请输入">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">角色</label>
        <div class="col-sm-4">
            <select id="roleId">
                <option th:each="role : ${roleList}" th:value="${role.id}" th:text="${role.name}"
                        th:selected="${rid.equals(role.id)}"></option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="button" class="btn btn-success" onclick="updateById()">确定</button>
        </div>
    </div>
</form>
</body>
<script>
    // console.log([[${admin}]]);
    // console.log([[${rid}]]);
    function updateById() {
        var roleId = $("#roleId option:checked").val();       //获取下拉框的value
        // console.log(rids);
        // console.log($("#username").val());
        var admin = {
            "username": $("#username").val(),
        }
        var j_admin = JSON.stringify(admin);
        // console.log(j_admin);
        $.ajax({
            url: "/admin/" + $("#id").val() + "?rid=" + roleId,
            type: "put",
            dataType: "json",
            data: j_admin,
            contentType: 'application/json;charset=UTF-8',
            success: function (result) {
                console.log(result);
                if (result.code == 200) {
                    toastr.success("操作成功");
                    setTimeout(function () {
                        parent.location.reload();
                    }, 1000);
                } else {
                    toastr.success("系统异常");
                }
            },
            error: function (e) {
                console.log(e);
                toastr.error("系统异常");
            }
        })
    }
</script>
</html>